package com.qianfeng.controller;

import com.qianfeng.dto.Page;
import com.qianfeng.dto.UserInfo;
import com.qianfeng.entity.Role;
import com.qianfeng.entity.User;
import com.qianfeng.service.RoleService;
import com.qianfeng.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;



    @RequestMapping("/getUserList")
    public String  getUserList(){
       /* System.out.println("===");*/
        return "10_user_management";
    }
    @ResponseBody
    @RequestMapping("/getUserPage")
    public Page getUserPage(Integer pageNo,Integer pageSize){
        Page page = userService.getUserList(pageNo, pageSize);
        return page;
    }
    @RequestMapping("/jumpAddUser")
    public String jumpAddUser(Map map){
        List<Role> roleList = roleService.getRoleList();
        map.put("roles",roleList);
        return "10_01_user_management_add";
    }

    @RequestMapping("/addUser")
    public String addUser(User user,Integer roleId){
        user.setCreateTime(new Date());
        userService.addUser(user,roleId);
        return "redirect:/getUserList";
    }
    @RequestMapping("/deleteUser/{userId}")
    public String deleteUser(@PathVariable("userId") Integer userId){
        userService.deleteUser(userId);
        return "redirect:/getUserList";
    }
    @RequestMapping("/jumpUpdateUser")
    public String jumpUpdateUser(Map map,Integer userId){
        System.out.println("userId==="+userId);
        User user = userService.getUserByUserId(userId);
        List<Role> roleList = roleService.getRoleList();
        map.put("user",user);
        map.put("roles",roleList);
        return "10_02_user_management_update";
    }
    @RequestMapping("/updateUser")
    public String updateUser(User user,Integer roleId){
        userService.updateUser(user,roleId);
        return "redirect:/getUserList";
    }
    @ResponseBody
    @RequestMapping("/getUserLikeList")
    public List<UserInfo> getUserLikeList(String userName){
       return userService.getUserLikeList(userName);
    }




}
